package com.beiding.pojo.order;

import com.beiding.pojo.Resource;
import com.beiding.pojo.room.Price;
import com.beiding.pojo.room.Room;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Calendar;

@Getter
@Setter
@Document
public class Order implements Resource {

    //创建
    public static final int CREATE = 0;

    //付款
    public static final int PAID = 1;

    //被受理
    public static final int ACCEPTED = 2;

    //使用中
    public static final int USING = 3;

    //到期
    public static final int EXPIRE = 4;

    //退押金
    public static final int REFUND_DEPOSIT = 5;

    //等待评价状态
    public static final int EVALUATE = 6;

    //订单终结
    public static final int TERMINATION = 7;

    //非正常状态

    //退款申请
    public static final int REFUND = -1;

    //退款分歧
    public static final int REFUND_DISPUTE = -2;

    //退款仲裁
    public static final int REFUND_ARBITRATION = -3;

    //订单被锁死
    public static final int LOCK = -4;

    //订单失效
    public static final int OVERDUE = -5;

    //扣押金申请
    public static final int WITHHOLD_DEPOSIT = -6;

    //扣押金申请分歧
    public static final int WITHHOLD_DEPOSIT_DISPUTE = -7;

    //扣押金仲裁
    public static final int WITHHOLD_DEPOSIT_ARBITRATION = -8;


    @Id
    //订单id
    private String id;

    //订单对应的房间id
    private String roomId;

    //订单对应的房间
    private Room room;

    //房间退款的原因
    private String refundReason;

    //扣押金原因
    private String withholdDepositReason;

    //计价数量
    private Integer priceCount;

    //申请退款的次数
    private Integer refundTime = 0;

    //计价编号.计价编号无法获取
    private Integer priceSerial;

    //房客的入住码.
    private String checkInKey;


    /*

        房客凭入住码进行入住.在房客办理入住时,房东应该向房客索取入住码.

        如果,房东连续3次输错入住码,则该订单将被锁死.如果累积6次输错,则该订单视为作废,之后将启用将启动退款流程

     */
    //入住码输入错误次数
    private Integer checkInKeyErrorTime = 0;

    //计价方式.由房间和计价编号决定
    private Price price;

    //* 计价编号比在它在List中的索引大1
    public void setRoom(Room room) {
        this.room = room;
        this.landlord = room.getHolderId();
        if (price == null && priceSerial != null) {
            setPrice(room.getPrices().get(priceSerial - 1));
        }
    }

    public void setPriceSerial(Integer priceSerial) {
        this.priceSerial = priceSerial;
        if (price == null && room != null) {
            setPrice(room.getPrices().get(priceSerial - 1));
        }
    }

    //房间的总消费.由计价方式和计价数量共同决定
    private Float cost;

    //计算房间总花费
    private void calculateCost() {
        cost = price.getPrice() * priceCount;
    }

    public void setPrice(Price price) {
        this.price = price;

        if (price != null) {


            if (price.getDeposit() != 0) {
                this.deposit = price.getDeposit();
            }

            if (cost == null && priceCount != null) {
                calculateCost();
            }
        }

    }

    public void setPriceCount(int priceCount) {
        this.priceCount = priceCount;
            if (cost == null && price != null) {
                calculateCost();
            }

    }


    //订单归房客所有
    private String tenant;

    //房间归房东所有
    private String landlord;

    //订单的状态
    private Integer state;

    //订单的押金
    private Float deposit;

    //扣留的金额
    private Float depositWithhold;

/*
    public void setDepositWithhold(Float depositWithhold) {

        Assert.notNull(this.deposit,"订单没有押金,扣留无效");
        Assert.isTrue(depositWithhold<=deposit,"扣留金额必须小于房间押金");
        this.depositWithhold = depositWithhold;
    }*/

    //订单创建时间
    private Long createTimestamp;

    //支付时间
    private Long paidTimestamp;

    //受理时间
    private Long acceptedTimestamp;

    //房间开始使用
    private Long useTimestamp;

    //房间到期时间戳
    private Long expireTimestamp;

    private Long usingMilliseconds;


    //设置房间开始使用的时间戳时,同时设置房间到期时间戳
    public void setUseTimestamp(Long useTimestamp) {
        this.useTimestamp = useTimestamp;

        if (useTimestamp != null) {

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(useTimestamp);
            calendar.add(price.getTimeUnit(), priceCount * price.getNum());

            //同时设置房间到期时间戳
            expireTimestamp = calendar.getTime().getTime();

            usingMilliseconds = expireTimestamp - useTimestamp;
        }

    }


    //平台费率
    private double platformCostRate;

    @Override
    public String getHolderId() {
        return tenant;
    }

/*
    //平台费用
    public float platformCost() {
        //采用收尾法精确到小数点后两位
        return new BigDecimal(platformCostRate* getCost()).setScale(2, RoundingMode.FLOOR).floatValue();
    }

    //房东收益
    public float landlordObtain() {
        return getCost() - platformCost();
    }
*/

}
